package com.fortune.service.impl;

import com.fortune.dao.DStoreOutDao;
import com.fortune.dao.DproductStockDetailInfoDao;
import com.fortune.dto.in.dStoreOut.DStoreOutDetailDto;
import com.fortune.dto.in.dStoreOut.DStoreOutListDto;
import com.fortune.exception.db.UpdateInnerErrorException;
import com.fortune.vo.dStoreIn.StorageDetails;
import com.fortune.vo.dStoreOut.DStorePro;
import com.fortune.entity.DUserInfo;
import com.fortune.service.DStoreOutService;
import com.fortune.vo.dStoreIn.MData;
import com.fortune.vo.dStoreOut.DStoreOutInfo;
import com.fortune.vo.dStoreOut.DStoreOutInfoDetail;
import com.fortune.vo.dStoreOut.DstoreId;
import com.fortune.vo.productStockDetailInfo.ProductStockDetailInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DStoreOutServiceImpl implements DStoreOutService {
    @Autowired
    private DStoreOutDao dStoreOutDao;
    @Autowired
    private DproductStockDetailInfoDao dproductStockDetailInfoDao;

    /**
     * 查询出库单列表
     * @return
     */
    @Override
    public List<DStoreOutInfo> queryDStoreOutList(DUserInfo dUserInfo, DStoreOutListDto dStoreOutListDto) {
        Date date = dStoreOutListDto.getDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStart;
        String dateEnd;
        if(date != null){
            dateStart = sdf.format(date) + " "+"00:00:00";
            dateEnd = sdf.format(date) +" "+"23:59:59";
        }else{
            dateStart = "";
            dateEnd = "";
        }
        return dStoreOutDao.queryDStoreOutList(dUserInfo,dStoreOutListDto,dateStart,dateEnd);
    }

    /**
     * 查询出库单列表个数
     * @return
     */
    @Override
    public int queryDStoreOutListCount(DUserInfo dUserInfo, DStoreOutListDto dStoreOutListDto) {
        return dStoreOutDao.queryDStoreOutListCount(dUserInfo,dStoreOutListDto);
    }

    @Override
    public List<DStorePro> findListDetail(DStoreOutDetailDto dStoreOutDetailDto,String did) {
        List<DStorePro> lists = new ArrayList<>();
        List<DstoreId> list = dStoreOutDetailDto.getId();
        for (int i =0; i<list.size(); i++){
            DstoreId dstoreId = list.get(i);
             List<DStorePro> aa = dStoreOutDao.findListDetail(dstoreId.getId(),did);
             for (DStorePro dStorePro:aa){
                    lists.add(dStorePro);
             }
        };
        return lists;
    }

    /**
     * 出库单详情
     * @param id
     * @return
     */
    @Override
    public DStoreOutInfoDetail queryDStoreOutInfoDetail(DUserInfo dUserInfo,String id) {
        return dStoreOutDao.queryDStoreOutInfoDetail(dUserInfo,id);
    }
    /**
     * 确定发货
     * @return
     */
    @Override
    @Transactional
    public int updateDStoreOutShipType(DUserInfo dUserInfo,DStoreOutDetailDto dStoreOutDetailDto) {
        List<DstoreId> lists= dStoreOutDetailDto.getId();
        if(lists.size()<0){
            return -2;
        }
        for (DstoreId dstoreId : lists){
            int num = dStoreOutDao.updateType(dUserInfo,dstoreId.getId());
            if (num > 0){
                List<StorageDetails> list = dStoreOutDao.findDetails(dUserInfo.getdId(),dstoreId.getId());
                for (int i = 0; i < list.size(); i++){
                    StorageDetails storageDetails = list.get(i);
                    int order = dproductStockDetailInfoDao.queryMax(dUserInfo.getdId(),storageDetails.getStoreId(),storageDetails.getMpid());
                    List<ProductStockDetailInfo> lastDate = dproductStockDetailInfoDao.queryLastStockDetail(dUserInfo.getdId(),storageDetails.getStoreId(),storageDetails.getMpid());
                    if (!lastDate.isEmpty()){
                        storageDetails.setRemainNum(lastDate.get(0).getRemainNum()+storageDetails.getInNum());
                    }else {
                        storageDetails.setRemainNum(storageDetails.getInNum());
                    }
                    if(storageDetails.getStoreId() == null){
                        return -99;
                    }
                    num = dStoreOutDao.insertStorageProductStockDetail(storageDetails.getMid(),dUserInfo.getdId(),storageDetails,order);
                    if (num < 1){
                        throw new RuntimeException("确定发货失败");
                    }
                }
                return 1;
            }else{
                throw new UpdateInnerErrorException("确定发货失败");
            }
        }
        return 1;
    }

    @Override
    public List<DStorePro> findListDidDriver(String did) {
        return dStoreOutDao.findListDidDriver(did);
    }

    @Override
    public List<DStorePro> listDidClient(String did) {
        return dStoreOutDao.findListDidClient(did);
    }


}
